<?php

/**
 * This is the model class for table "tbl_user".
 *
 * The followings are the available columns in table 'tbl_user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $displayname
 * @property string $position
 * @property integer $active
 * @property string $email
 * @property string $mobile
 * @property string $createddate
 * @property string $modifieddate
 * @property integer $isadmin
 *
 * The followings are the available model relations:
 * @property Configuration[] $configurations
 */
class User extends CActiveRecord
{
	public $password2;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, displayname', 'required'),
			array('username', 'unique'),
			array((isset($this->id) && $this->id > 0) ? "username" : 'password2', 'required'),
			array('active, isadmin', 'numerical', 'integerOnly'=>true),
			array('username, password, displayname, email', 'length', 'max'=>128),
			array('username','length','min'=>4),
			array('password2','length','min'=>8),
			array('position', 'length', 'max'=>255),
			array('mobile', 'length', 'max'=>20),
			array('createddate, modifieddate', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, username, password, displayname, position, active, email, mobile, createddate, modifieddate, isadmin', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'configurations' => array(self::HAS_MANY, 'Configuration', 'user'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'User Name',
			'password' => 'Password',
			'password2' => 'Password',
			'displayname' => 'Display Name',
			'position' => 'Position',
			'active' => 'Active',
			'email' => 'Email',
			'mobile' => 'Mobile',
			'createddate' => 'Createddate',
			'modifieddate' => 'Modifieddate',
			'isadmin' => 'Is Admin',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('displayname',$this->displayname,true);
		$criteria->compare('position',$this->position,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('createddate',$this->createddate,true);
		$criteria->compare('modifieddate',$this->modifieddate,true);
		$criteria->compare('isadmin',$this->isadmin);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
